package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class TomNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		//http://post.news.tom.com/s/D7000B197143.html
		//http://tom.society.people.com.cn/n/2012/1122/c1008-19658456.html
		//http://tom.legal.people.com.cn/n/2012/1122/c42510-19657904.html?source=people_NEWS
		//http://news.tom.com/2012-11-22/OKVF/82735683.html
		//http://yule.tom.com/2012-04-24/1239/01782759.html
		//http://star.tom.com/2012-11-22/000E/07822989.html
		//http://view.tom.com/vw/332253-1.html  没有时间属性，干掉不要。
		//http://ittom.itcpn.net/317366/783150352818b.shtml
//		/http://news.tom.com/2014-01-10/OKVF/82753626.html?source=HP_TOPIC
		boolean bRet=false;
		//"http://tom[.].*[.]people[.]com[.]cn/.*/[0-9]{4}/[0-9]{4}/c[0-9]*[-][0-9]*[.]html.*",
		String[] regex={
				
				".*[.]tom[.]com/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9A-Z]{4}/[0-9]*[.]html",
				"http://post[.].*tom[.]com/s/[0-9A-Z]*[.]html",
				"http://tom[.].*[.]ynet[.]com/.*/[0-9]{4}/[0-9]{2}/[0-9]*[.]html.*",
				"http://.*[.]itcpn[.]net/[0-9]*/[0-9]*b[.]shtml",
				"http://.*.tom.com/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*/[0-9]*[.]html.*",
				"http://society.people.com.cn/n/2014/0110/c1008-24075898.html",
				"http://auto.tom.com/[0-9]{4}/[0-9]{4}/[0-9]*.html"};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}
	
	// 匹配日期
	private static final Pattern DATE=Pattern
			.compile("[0-9]{4}[年|-][0-9]{1,2}[月|-][0-9]{1,2}[日]{0,1}");



	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String html=urlMeta.getHtml();
		if ( html== null) {
		}
		String title="";
		String content="";
		String url=urlMeta.getUrl();
		Long date=null; 
		String source="TOM";
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		
		if(StringUtils.isBlank(title)){
			title= doc.select("div#PrintTxt>h1").text();
			if(StringUtils.isBlank(title)){
				title= doc.select("div.content>h1").text();
				if(StringUtils.isBlank(title)){
					title= doc.select("div.content>h1").text();
					if(StringUtils.isBlank(title)){
						title= doc.select("div.va>h1").text();
						if(StringUtils.isBlank(title)){
							title= doc.select("h1.nry_wzb").text();
							if(StringUtils.isBlank(title)){
								title= doc.select("title").text();
								if(title.indexOf("_")!=-1){
									title=StringUtils.substringBefore(title, "_");
								}
							}
						}
					}
				}
			}
		}

		
		
		
			
		
		
		/*
		 * 解析时间 
		 */
		date=DateUtils.matchDate(doc.select("span#p_publishtime").text());
		if(date==null){
			date=DateUtils.matchDate( doc.select("div.newsinfo").text());
			if(date==null){
				date=DateUtils.matchDate( doc.select("div.va>p.pd10").text());
				if(date==null){
					 String text=doc.select("span.zecc").text();
					 Matcher matchDate=DATE.matcher(text);
						if (matchDate.find()) {
							String group=matchDate.group();
							date=DateUtils.FormString(group);
						}
				}
			}
		}
		if(date==null){
			date=DateUtils.matchDate(url);
		}

		
		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div#zoom"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#content_body"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div#pzoom>p"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.nry_zw"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div.description"));
					}
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div#endtext"));
					}
				}
			}
			
		}
		
		
		if(StringUtils.isBlank(title)){
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);
		
		if(url.matches("http://post[.].*tom[.]com/s/[0-9A-Z]*[.]html")){
			meta.setUpdateUrl(url);
			String ding=doc.select("span#digg_article_num").text();
			String cai=doc.select("span#trample_article_num").text();
			String shoucang= doc.select("span#fav_num").text();
			int iDing =0;
			int iCai=0;
			int iShoucang=0;
			try {
				 iDing=Integer.parseInt(ding);
			} catch (NumberFormatException e1) {
				// TODO Auto-generated catch block
			}
			try {
				 iCai=Integer.parseInt(cai);
			} catch (NumberFormatException e1) {
				// TODO Auto-generated catch block
			}
			try {
				 iShoucang=Integer.parseInt(shoucang);
			} catch (NumberFormatException e1) {
				// TODO Auto-generated catch block
			}
			try {
				
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
			}
			meta.setCommentNum(iDing+iCai+iShoucang);
		}
		String author=null;
		author=doc.select("div.content div.newsinfo").text();
		if(author.indexOf("来源：")!=-1){
			author=author.substring(author.indexOf("来源：")+"来源：".length());
			if(author.indexOf(" ")!=-1){
				author=author.substring(0,author.indexOf(" "));
			}
		}
		if(StringUtils.isBlank(author)){
			author=doc.select("div.description span#SourceName").text();
			author=author.replace("来源:", "");
//			author=author.trim();
		}
		meta.setAuthor(author);

		
		return meta;
	}
	
	public static void main(String[] args) {
		String url="http://auto.tom.com/2014/0811/119797.html";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		TomNewsAnalyse cnhubeiNewsAnalyse=new TomNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if(detailPage){
			NewsMeta parserHtml =cnhubeiNewsAnalyse.parserHtml(urlMeta);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta!=null) {
			String updateUrl=meta.getUpdateUrl();
			if (updateUrl
					.matches("http://post[.].*tom[.]com/s/[0-9A-Z]*[.]html")) {
				UrlMeta responseToURL=CrawlHTML.responseToURL(updateUrl);
				String html=responseToURL.getHtml();
				Document doc=Jsoup.parse(html);
				String ding=doc.select("span#digg_article_num").text();
				String cai=doc.select("span#trample_article_num").text();
				String shoucang=doc.select("span#fav_num").text();
				int iDing=0;
				int iCai=0;
				int iShoucang=0;
				try {
					iDing=Integer.parseInt(ding);
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
				}
				try {
					iCai=Integer.parseInt(cai);
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
				}
				try {
					iShoucang=Integer.parseInt(shoucang);
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
				}
				try {

				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
				meta.setCommentNum(iDing + iCai + iShoucang);
				return meta;
			}
		}
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
